home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / CBEXPORT.C < prev    next >
Text File  |  1991-09-23  |  4KB  |  180 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbexport.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13. #include <stdio.h>
  14. #ifdef AC_STDLIB
  15. #include <stdlib.h>
  16. #endif
  17.  
  18. /* local headers */
  19. #include "cbase_.h"
  20.  
  21. /*man---------------------------------------------------------------------------
  22. NAME
  23.      cbexport - export cbase data
  24.  
  25. SYNOPSIS
  26.      #include <cbase.h>
  27.  
  28.      int cbexport(cbp, filename)
  29.      cbase_t *cbp;
  30.      const char *filename;
  31.  
  32. DESCRIPTION
  33.      The cbexport function exports all records in cbase cbp to a
  34.      printable file.  filename points to a character string that
  35.      contains the name of the file to receive the data.  If the file
  36.      does not exist, it is created.  If the file exists, it is
  37.      truncated to zero length before the data is exported.
  38.  
  39.      The exported file format is as follows:
  40.  
  41.           o Each record is terminated by a newline ('\\n').
  42.           o The fields in a record are delimited by vertical
  43.             bars ('|').
  44.           o Each field contains only printable characters.
  45.           o If a field contains the field delimiter
  46.             character, that character is replaced with \\F.
  47.           o The individual elements of array data types are
  48.             exported as individual fields.
  49.  
  50.      cbexport will fail if one or more of the following is true:
  51.  
  52.      [EINVAL]       cbp is not a valid cbase pointer.
  53.      [EINVAL]       filename is the NULL pointer.
  54.      [CBELOCK]      cbp is not read-locked.
  55.      [CBENOPEN]     cbp is not open.
  56.      [CBEPRFILE]    Error writing to printable file.
  57.  
  58. DIAGNOSTICS
  59.      Upon successful completion, a value of 0 is returned.  Otherwise,
  60.      a value of -1 is returned, and errno set to indicate the error.
  61.  
  62. ------------------------------------------------------------------------------*/
  63. #ifdef AC_PROTO
  64. int cbexport(cbase_t *cbp, const char *filename)
  65. #else
  66. int cbexport(cbp, filename)
  67. cbase_t *cbp;
  68. const char *filename;
  69. #endif
  70. {
  71.     int    rs    = 0;
  72.     int    terrno    = 0;
  73.     FILE *    fp    = NULL;
  74.     void *    buf    = NULL;
  75.     int    field    = 0;
  76.     size_t    fldos    = 0;
  77.     size_t    fldlen    = 0;
  78.     int    fldtype    = 0;
  79.  
  80.     /* validate arguments */
  81.     if (!cb_valid(cbp) || filename == NULL) {
  82.         errno = EINVAL;
  83.         return -1;
  84.     }
  85.  
  86.     /* check if not read locked */
  87.     if (!(cbp->flags & CB_RDLCK)) {
  88.         errno = CBELOCK;
  89.         return -1;
  90.     }
  91.  
  92.     /* truncate or create file for writing */
  93.     fp = fopen(filename, "w");
  94.     if (fp == NULL) {
  95.         return -1;
  96.     }
  97.  
  98.     /* check if cbp is empty */
  99.     if (cbreccnt(cbp) == 0) {
  100.         if (fclose(fp) == EOF) {
  101.             CBEPRINT;
  102.             return -1;
  103.         }
  104.         return 0;
  105.     }
  106.  
  107.     /* position record cursor to first record */
  108.     if (cbrecfirst(cbp) == -1) {
  109.         CBEPRINT;
  110.         terrno = errno;
  111.         fclose(fp);
  112.         errno = terrno;
  113.         return -1;
  114.     }
  115.  
  116.     /* export all records to printable file */
  117.     buf = calloc((size_t)1, cbrecsize(cbp));
  118.     if (buf == NULL) {
  119.         CBEPRINT;
  120.         fclose(fp);
  121.         errno = ENOMEM;
  122.         return -1;
  123.     }
  124.     while (cbrcursor(cbp) != NULL) {
  125.         if (cbgetr(cbp, buf) == -1) {    /* read current record */
  126.             CBEPRINT;
  127.             terrno = errno;
  128.             free(buf);
  129.             fclose(fp);
  130.             errno = terrno;
  131.             return -1;
  132.         }
  133.         /* output each field of current record */
  134.         for (field = 0; field < cbp->fldc; ++field) {
  135.             fldos = cbp->fldv[field].offset;
  136.             fldlen = cbp->fldv[field].len;
  137.             fldtype = cbp->fldv[field].type;
  138.             /* write field to printable file */
  139.             if ((*cbexpv[fldtype])(fp, (char *)buf + fldos, fldlen) == -1) {
  140.                 CBEPRINT;
  141.                 free(buf);
  142.                 fclose(fp);
  143.                 errno = CBEPRFILE;
  144.                 return -1;
  145.             }
  146.             if (field < cbp->fldc - 1) {
  147.                 rs = fputc(EXPFLDDLM, fp);
  148.             } else {
  149.                 rs = fputc(EXPRECDLM, fp);
  150.             }
  151.             if (rs == EOF) {
  152.                 CBEPRINT;
  153.                 terrno = errno;
  154.                 free(buf);
  155.                 fclose(fp);
  156.                 errno = terrno;
  157.                 return -1;
  158.             }
  159.         }
  160.         if (cbrecnext(cbp) == -1) {
  161.             CBEPRINT;
  162.             terrno = errno;
  163.             free(buf);
  164.             fclose(fp);
  165.             errno = terrno;
  166.             return -1;
  167.         }
  168.     }
  169.     free(buf);
  170.     buf = NULL;
  171.  
  172.     /* close file */
  173.     if (fclose(fp) == EOF) {
  174.         CBEPRINT;
  175.         return -1;
  176.     }
  177.  
  178.     return 0;
  179. }
  180.